var webpack = require("webpack");
var webpackconfig = require("./webpack.config.js");

module.exports = function(grunt) {
	// 项目配置
	grunt.initConfig({
		pkg: grunt.file.readJSON("package.json"),
		clean: {
			build: {
				src: ["dist/**/*", "prod/**/*", "apistore.zip", "dist.zip", "html.zip"], //清空文件和文件夹
			},
		},
		webpack: {
			//调用webpack功能进行编译构建
			options: webpackconfig,
			prod: {
				//产品构建
				devtool: "#source-map",
				plugins: webpackconfig.plugins.concat(
					new webpack.DefinePlugin({
						"process.env": {
							NODE_ENV: '"production"',
						},
					}),
					new webpack.optimize.UglifyJsPlugin({
						compress: {
							warnings: false,
						},
					})
				),
			},
			dev: {
				//开发环境构建
				devtool: "#eval-source-map",
				plugins: webpackconfig.plugins.concat(
					new webpack.optimize.DedupePlugin(),
					new webpack.optimize.UglifyJsPlugin({
						compress: {
							warnings: false,
						},
					})
				),
			},
		},
		"webpack-dev-server": {
			//开发调试，实时编译
			options: {
				webpack: webpackconfig,
				publicPath: webpackconfig.output.publicPath,
			},
			start: {
				keepalive: true,
				port: 8086,
				historyApiFallback: true,
				noInfo: true,
				inline: true,
				hot: true,
				compress: true,
				watchOptions: {
					aggregateTimeout: 300,
					poll: 1000,
				},
			},
		},
		copy: {
			//拷贝文件
			common: {
				files: [
					{ expand: true, src: ["package.json"], dest: "prod/" },
					{ expand: true, src: ["index.html"], dest: "prod/" },
					{ expand: true, src: ["dist/**/*"], dest: "prod/" },
				],
			},
			http2: {
				files: [
					{ expand: true, src: ["http2.js"], dest: "prod/" },
					{ expand: true, src: ["ssl/**/*"], dest: "prod/" },
				],
			},
			http: {
				files: [
					{ expand: true, src: ["http.js"], dest: "prod/" },
					{ expand: true, src: ["mine.js"], dest: "prod/" },
					{ expand: true, src: ["server.js"], dest: "prod/" },
				],
			},
		},
		zip: {
			//打zip包
			apistore: {
				dest: "apistore.zip",
				src: ["prod/**/*"],
			},
			dist: {
				dest: "dist.zip",
				src: ["dist/**/*"],
			},
			html: {
				dest: "html.zip",
				src: ["dist/**/*.js", "dist/**/*.css"],
			},
		},
		shell: {
			//shell命令
			options: {
				stderr: true,
			},
			dev: {
				command: "npm run dev",
			},
		},
	});
	grunt.loadNpmTasks("grunt-contrib-requirejs");
	grunt.loadNpmTasks("grunt-webpack");
	grunt.loadNpmTasks("grunt-contrib-clean");
	grunt.loadNpmTasks("grunt-contrib-imagemin");
	grunt.loadNpmTasks("grunt-zip");
	grunt.loadNpmTasks("grunt-shell");
	grunt.loadNpmTasks("grunt-contrib-copy");

	grunt.registerTask("ziph2", ["clean", "webpack:prod", "copy:common", "copy:http2", "zip:apistore"]);
	grunt.registerTask("ziphttp", ["clean", "webpack:prod", "copy:common", "copy:http", "zip:apistore"]);
	grunt.registerTask("zipall", ["clean", "webpack:prod", "copy:common", "copy:http", "copy:http2", "zip:apistore"]);
	grunt.registerTask("build-dev", ["clean", "webpack:dev"]);
	grunt.registerTask("build", ["clean", "webpack:prod"]);
	grunt.registerTask("default", ["webpack-dev-server"]);
};
